package com.kzd.gtgame.main.access

import android.text.method.LinkMovementMethod
import androidx.core.widget.addTextChangedListener
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.toAgreeLinks
import com.kzd.gtgame.jetpack.vm.AccessViewModel
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_access_login.*

class AccessLoginFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): AccessLoginFragment {
            return AccessLoginFragment()
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_login

    override fun initView() {

        tvAccessLoginPact.text = resources.getString(R.string.app_pact1).toAgreeLinks(activity = requireActivity())
        tvAccessLoginPact.movementMethod = LinkMovementMethod.getInstance()

        etAccessLoginAccount.addTextChangedListener {
            tvAccessLoginSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessLoginPassword.text.isNullOrEmpty()
        }

        etAccessLoginPassword.addTextChangedListener {
            tvAccessLoginSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessLoginAccount.text.isNullOrEmpty()
        }

        tvAccessLoginSubmit.setOnClickAntiShakeListener {
            viewModel.accessLoginName(
                etAccessLoginAccount.text.toString(),
                etAccessLoginPassword.text.toString()
            )
        }
    }

}